package bb.lanxing.model.database;

import android.text.TextUtils;
//import com.orm.SugarRecord;
//import com.orm.dsl.Table;
//import com.orm.query.Select;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.common.config.Constants;
import bb.lanxing.util.Log;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

public class Province extends LitePalTableBase {
    private static final String CITY_INSERT = "INSERT INTO province (id, name, citycount) VALUES ";
    private int cityCount;
    private String name;

    public static void importDatas2() {
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(App.getContext().getAssets().open("province.txt")));
            while (true) {
                String readLine = bufferedReader.readLine();
                if (readLine == null) {
                    return;
                }
                LitePal.getDatabase().execSQL(CITY_INSERT + readLine);
                Log.d("province file sql : " + readLine);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void importDatas() {
        String string = App.getContext().getString(R.string.province_data_insert_sql);
        Log.d(Constants.TAG, "province sql : " + string);
        try {
            LitePal.getDatabase().execSQL(string);
        } catch (Exception unused) {
            importDatas2();
        }
    }

    public static Province getById(int i) {

        String id = String.valueOf(i);
        Province p = LitePal.where("id=?", id).findFirst(Province.class);
        return p;


//        return (Province) Select.from(Province.class).where("id = ?", new String[]{String.valueOf(i)}).first();
    }

    public static long getProvinceIdByName(String str) {
        if (TextUtils.isEmpty(str)) {
            return 0L;
        }

        Province province = LitePal.where("name like ?", str).findFirst(Province.class);

//        Select from = Select.from(Province.class);
//        Province province = (Province) from.where("name like \"" + str + "%\"").first();


        if (province != null) {
            return province.getBaseObjId();
        }
        return 0L;
    }

    public static List<Province> getAll() {

        List<Province> l = LitePal.findAll(Province.class);
        return l;

//        return Select.from(Province.class).list();
    }

    public static long getCount() {

        long c = LitePal.count(Province.class);
        return c;

//        return Select.from(Province.class).count();
    }


    public void setName(String name) {
        this.name = name;
    }

    public void setCityCount(int cityCount) {
        this.cityCount = cityCount;
    }

    public String getName() {
        return this.name;
    }

    public int getCityCount() {
        return this.cityCount;
    }


}
